package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@Api(tags = "菜品相关接口")
@RestController
@RequestMapping("/admin/dish")

public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result saveDish(@RequestBody DishDTO dishDTO) {
        //todo 删除=> dish+分类
        String CACHE_KEY = "DISH_" + dishDTO.getCategoryId();
        dishService.saveDishAndFlavor(dishDTO);
        redisTemplate.delete(CACHE_KEY);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        //1、页面展示数据（dish表、分类名称）
        //2、去数据库查询
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("根据id删除一条或多条")
    @DeleteMapping
    public Result delByIds(@RequestParam List<Long> ids) {
        //多个菜可能对象多个分类
        //全删
        //查询所以相关的key
        String SEARCH_KEY = "DISH_*";
        Set keys = redisTemplate.keys(SEARCH_KEY);
        //把所以的菜品缓存全部删除
        redisTemplate.delete(keys);
        dishService.delByIds(ids);
        return Result.success();
    }

    /**
     * 根据菜品id查找菜品信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查找菜品")
    @GetMapping("{list}")
    public Result<DishVO> findById(@PathVariable("list") Long id) {
        DishVO dishVo = dishService.findById(id);
        return Result.success(dishVo);
    }


    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        String DISH_UPDATE = "DISH_" + dishDTO.getCategoryId();
        redisTemplate.delete(DISH_UPDATE);
        return dishService.update(dishDTO);
    }


    /**
     * 菜品启售或停售
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("起售或停售")
    @PostMapping("/status/{status}")
    public Result<String> enableOrDisable(@PathVariable("status") Integer status, Long id) {
        DishVO dishVO = dishService.findById(id);
        String DISH_SRART = "DISH_" + dishVO.getCategoryId();
        redisTemplate.delete(DISH_SRART);

        dishService.enableOrDisable(status, id);
        return Result.success();
    }

    /**
     * 根据id分类查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据id分类查询菜品")
    public Result<List<Dish>> findByCategoryId(Long categoryId) {
        List<Dish> list = dishService.findByCategoryId(categoryId);
        return Result.success(list);
    }
}
